<?php
namespace Home\Model;
use Think\Model;

class RunModel extends Model {

    protected $insertFields = array('client_id', 'give_name', 'last_time', 'run_time', 'status', 'step', 'why_run');
    protected $updateFields = array('id', 'give_name', 'last_time', 'run_time', 'status', 'step', 'why_run');
    protected $_validate = array(
        array('why_run', 'require', '流失原因不能为空！', 0, 'regex', 3),
    );
    public $_step = array(
        array('step', 'require', '暂缓措施不能为空！', 0, 'regex', 4),
        array('step', '1,65535', '暂缓措施总共能存65535个字段！', 0, 'length', 4),
    );
    public $_why_run = array(
        array('why_run', 'require', '流失原因不能为空！', 0, 'regex', 5),
        array('why_run', '1,65535', '流失原因总共能存65535个字段！', 0, 'length', 5),
    );

    public function search($isrun=NULL , $pageSize = 20) {
        /********************************** 搜索 ******************************************/
         /***********************客户经理限制权限 ************************* */
        $where = array();
        $sid = session('id');
        $sname = session('username');
        $adModel = M('Admin_role');
        $rid = $adModel->field('role_id')->where(array('admin_id' => array('eq', $sid)))->find();
        if ($rid['role_id'] == '4') {
            $where['give_name'] = array('eq', "$sname");
        }
        
        $where = array();
        $where['last_time'] =  array('lt',time()-3600*24*30*6);
        if($isrun != NULL)
            $where['run_time'] = array('gt', 3600);
        if ($name = I('get.name'))
            $where['name'] = array('like', "%$name%");
        if ($give_name = I('get.give_name'))
            $where['give_name'] = array('like', "%$give_name%");
        if ($status = I('get.status'))
            $where['status'] = array('like', "%$status%");
        /******************************* 翻页 ***********************************/
        $count = $this->alias('a')->where($where)->count();
        $page = new \Think\Page($count, $pageSize);
        // 配置翻页的样式
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $data['page'] = $page->show();
        /***************************************** 取数据 ***************************************/
        $data['data'] = $this->alias('a')->join('left join crm_client b on a.client_id=b.id')
                        ->field('a.id,a.client_id,a.give_name,a.last_time,a.run_time,a.status,a.step,a.why_run,b.name')
                        ->where($where)->group('a.id')->limit($page->firstRow . ',' . $page->listRows)->select();
        return $data;
    }

    public function addStep($data, $id) {
        $sql = "UPDATE crm_run a SET a.step = CONCAT(a.step,'<br/>','" . $data . "') where a.id = $id";
        $ok = $this->query($sql);
        if ($ok)
            return TRUE;
    }

    // 添加前
    protected function _before_insert(&$data, $option) {
        
    }

    // 修改前
    protected function _before_update(&$data, $option) {
        
    }

    // 删除前
    protected function _before_delete($option) {
        
    }

    /******************************** 其他方法 ********************************************/
}
